package com.rceeslca.modules.cam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rceeslca.modules.cam.dao.CamAttenDao;
import com.rceeslca.modules.cam.dao.CamCourseItemDao;
import com.rceeslca.modules.cam.dao.CamUserDao;
import com.rceeslca.modules.cam.entity.CamCourseItem;
import com.rceeslca.modules.cam.service.CamAttenService;
import com.rceeslca.modules.cam.vo.AttenCourseVo;
import com.rceeslca.modules.cam.vo.CamUserVo;
import com.rceeslca.modules.security.user.UserDetail;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 考勤
 */
@Service
@AllArgsConstructor
public class CamAttenServiceImpl implements CamAttenService {

    @Autowired
    private CamUserDao camUserDao;

    @Autowired
    private CamAttenDao camAttenDao;

    @Autowired
    private CamCourseItemDao camCourseItemDao;


    /**
     * 分页查询考勤课程
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<AttenCourseVo> pageCourse(Page<AttenCourseVo> page, AttenCourseVo param) {

        //当前用户
        UserDetail user = (UserDetail) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        List<CamUserVo> userVoList = camUserDao.listUser(null, user.getId());
        if(userVoList!=null&&!userVoList.isEmpty()){
            CamUserVo camUserVo = userVoList.get(0);

            if(camUserVo.getRoleName()!=null){
                if(camUserVo.getRoleName().equals("学生")){
                    return getStuAttenCoursePage(page,camUserVo.getId(),param.getCourseName(),param.getTeacherName());
                }else if(camUserVo.getRoleName().equals("教师")){
                    return getTeaAdminAttenCoursePage(page,camUserVo.getId(),param.getCourseName(),param.getTeacherName());
                }else if(camUserVo.getRoleName().equals("管理员")){
                    return getTeaAdminAttenCoursePage(page,null,param.getCourseName(),param.getTeacherName());
                }else{
                    return null;
                }
            }else{
                if(camUserVo.getSuperAdmin()==1){
                    return getTeaAdminAttenCoursePage(page,null,param.getCourseName(),param.getTeacherName());
                }else{
                    return null;
                }
            }
        }else{
            return null;
        }
    }


    /**
     * 学生考勤课程
     */
    public IPage<AttenCourseVo> getStuAttenCoursePage(Page<AttenCourseVo> page,
                                                      Long userId,
                                                      String courseName,
                                                      String teacherName){

        IPage<AttenCourseVo> stuPage = camAttenDao.pageStuAttenCourse(page,userId,courseName,teacherName);
        for(AttenCourseVo record : stuPage.getRecords()){
            if(record.getOperation().equals("null")){
                record.setAttenStatus("未考勤");
            }
            if(record.getOperation().equals("sign")){
                record.setAttenStatus("已签到");
                record.setAttenTime(record.getOperationTime());
            }
            if(record.getOperation().equals("leave")){
                if(record.getOperationResult()!=null){
                    if(record.getOperationResult().equals("success")){
                        record.setAttenStatus("请假通过");
                    }else{
                        record.setAttenStatus("请假拒绝");
                    }
                }else{
                    record.setAttenStatus("请假中");
                }

                record.setAttenTime(record.getOperationTime());
            }

            record.setOperation(null);
            record.setOperationResult(null);
            record.setOperationTime(null);
        }

        return stuPage;
    }


    /**
     * 教师管理员考勤课程
     */
    public IPage<AttenCourseVo> getTeaAdminAttenCoursePage(Page<AttenCourseVo> page,
                                                           Long userId,
                                                           String courseName,
                                                           String teacherName){

        IPage<AttenCourseVo> coursePage = camAttenDao.pageTeaAdmAttenCourse(page,userId,courseName,teacherName);
        for(AttenCourseVo record : coursePage.getRecords()){

            QueryWrapper<CamCourseItem> ciWrapper = new QueryWrapper<>();
            ciWrapper.lambda().eq(CamCourseItem::getCourseId,record.getCourseId());
            List<CamCourseItem> courseItemList = camCourseItemDao.selectList(ciWrapper);

            int signNum = (int) courseItemList.stream().filter(item -> item.getOperation().equals("sign")).count();

            int leaveNum = (int) courseItemList.stream().filter(item -> item.getOperation().equals("leave")&&
                    (item.getOperationResult()!=null&&item.getOperationResult().equals("success"))).count();

            int noAttenNum = (int) courseItemList.stream().filter(item -> item.getOperation().equals("null")||
                    (item.getOperation().equals("leave")&&item.getOperationResult()!=null
                            &&!item.getOperationResult().equals("success"))).count();

            record.setSignNum(signNum);
            record.setLeaveNum(leaveNum);
            record.setNoAttenNum(noAttenNum);
        }

        return coursePage;
    }


    /**
     * 分页查询请假
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<AttenCourseVo> pageLeave(Page<AttenCourseVo> page, AttenCourseVo param) {

        //当前用户
        UserDetail user = (UserDetail) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        List<CamUserVo> userVoList = camUserDao.listUser(null, user.getId());
        if(userVoList!=null&&!userVoList.isEmpty()){
            CamUserVo camUserVo = userVoList.get(0);
            if(camUserVo.getRoleName()!=null){
                if(camUserVo.getRoleName().equals("学生")){
                    return getAttenLeave(page,camUserVo.getId(),null,param.getStudentName(),param.getTeacherName(),param.getCourseName());
                }else if(camUserVo.getRoleName().equals("教师")){
                    return getAttenLeave(page,null,camUserVo.getId(), param.getStudentName(), param.getTeacherName(), param.getCourseName());
                }else if(camUserVo.getRoleName().equals("管理员")){
                    return getAttenLeave(page,null,null, param.getStudentName(), param.getTeacherName(), param.getCourseName());
                }else{
                    return null;
                }
            }else{
                if(camUserVo.getSuperAdmin()==1){
                    return getAttenLeave(page,null,null, param.getStudentName(), param.getTeacherName(), param.getCourseName());
                }else{
                    return null;
                }
            }
        }else{
            return null;
        }
    }


    /**
     * 教师管理员考勤课程
     */
    public IPage<AttenCourseVo> getAttenLeave(Page<AttenCourseVo> page,
                                              Long studentId,
                                              Long teacherId,
                                              String studentName,
                                              String teacherName,
                                              String courseName){

        IPage<AttenCourseVo> coursePage = camAttenDao.pageAttenLeave(page,studentId,teacherId,studentName,teacherName,courseName);
        for(AttenCourseVo record : coursePage.getRecords()){
            if(record.getOperationResult()!=null){
                if(record.getOperationResult().equals("success")){
                    record.setAttenStatus("请假通过");
                }else{
                    record.setAttenStatus("请假拒绝");
                }
            }else{
                record.setAttenStatus("请假中");
            }

            record.setOperation(null);
            record.setOperationResult(null);
        }

        return coursePage;
    }


    /**
     * 选课详情
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<AttenCourseVo> pageCourseItem(Page<AttenCourseVo> page, AttenCourseVo param) {

        IPage<AttenCourseVo> itemPage = camAttenDao.pageCourseItem(page,param.getCourseId());

        for(AttenCourseVo record : itemPage.getRecords()){
            if(record.getOperation().equals("null")){
                record.setAttenStatus("未考勤");
            }
            if(record.getOperation().equals("sign")){
                record.setAttenStatus("已签到");
                record.setAttenTime(record.getOperationTime());
            }
            if(record.getOperation().equals("leave")){
                if(record.getOperationResult()!=null){
                    if(record.getOperationResult().equals("success")){
                        record.setAttenStatus("请假通过");
                    }else{
                        record.setAttenStatus("请假拒绝");
                    }
                }else{
                    record.setAttenStatus("请假中");
                }

                record.setAttenTime(record.getOperationTime());
            }

            record.setOperation(null);
            record.setOperationResult(null);
            record.setOperationTime(null);
        }

        return itemPage;
    }

}
